package com.feidee.data.email.dao.impl;

import com.feidee.data.email.dao.QueryDao;
import com.feidee.data.email.model.EConstant;
import com.feidee.data.report.exception.DataDaoException;
import com.feidee.data.report.util.JdbcManager;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("queryDaoImpl")
public class QueryDaoImpl implements QueryDao {


    @Resource(name = "hiveDataSource")
    private DataSource hiveSource;

  /*  @Resource(name = "impalaDataSource")
    private DataSource impalaSource;*/

 /* @Resource(name = "mysqlDataSource")
  private DataSource mysqlSource;
*/

    private DataSource sensitiveDataSource;

    @Override
    public List<Map<String, String>> queryWithSql(String sql, String engine) throws DataDaoException {
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        List<Map<String, String>> result;
        try {
            conn = getConnection(engine);
            stat = conn.createStatement();
            rs = stat.executeQuery(sql);
            ResultSetMetaData metaData = rs.getMetaData();
            List<String> colums = new ArrayList<String>();
            result = new ArrayList<Map<String, String>>();
            for (int i = 0; i < metaData.getColumnCount(); i++) {
                colums.add(metaData.getColumnName(i + 1));
            }
            while (rs.next()) {
                Map<String, String> data = new HashMap<String, String>();
                for (String colum : colums) {
                    data.put(colum, rs.getString(colum));
                }
                result.add(data);
            }
            return result;
        } catch (SQLException e) {
            throw new DataDaoException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DataDaoException(e.getMessage(), e);
        } finally {
            JdbcManager.destroy(conn, stat, rs);
        }
    }


    @Override
    public void createWithSql(String sql, String engine) throws DataDaoException {
        Connection conn = null;
        Statement stat = null;
        try {
            conn = getConnection(engine);
            stat = conn.createStatement();
            stat.execute(sql);
        } catch (SQLException e) {
            throw new DataDaoException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DataDaoException(e.getMessage(), e);
        } finally {
            JdbcManager.destroy(conn, stat, null);
        }
    }


    private Connection getConnection(String engine) throws SQLException {
        if (EConstant.HIVE.equals(engine)) {
            return hiveSource.getConnection();
        } /*else if (EConstant.IMPALA.equals(engine)) {
            return impalaSource.getConnection();
        } */else if (EConstant.MYSQL_DATA.equals(engine)) {
            //return mysqlSource.getConnection();
            return sensitiveDataSource.getConnection();
        }

        return null;
    }

    public void setSensitiveDataSource(DataSource sensitiveDataSource) {
        this.sensitiveDataSource = sensitiveDataSource;
    }
}
